package com.wiz.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.wiz.web.vo.criteria.FpCriteria;

public class SerializeUtil {
	
	public static byte[] serialize(Object object) {
		ObjectOutputStream oos = null;
		ByteArrayOutputStream baos = null;
		try {
			// 序列化
			baos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(baos);
			oos.writeObject(object);
			byte[] bytes = baos.toByteArray();
			return bytes;
		} catch (Exception e) {

		}
		return null;
	}

	public static Object unserialize(byte[] bytes) {
		ByteArrayInputStream bais = null;
		try {
			// 反序列化
			bais = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			return ois.readObject();
		} catch (Exception e) {

		}
		return null;
	}
	
	public static void main(String[] args) {
		List<FpCriteria> criteriaList = new ArrayList<FpCriteria>();
		for(int i=0;i<20;i++){
			FpCriteria cri = new FpCriteria();
			cri.setFpNo(i+"..");
			criteriaList.add(cri);
		}
		Gson gson = new Gson();
		
		long start = System.currentTimeMillis();
		for(int i=0;i<20;i++){
			gson.toJson(criteriaList.get(i));
		}
		System.out.println(System.currentTimeMillis()-start);
		
		start = System.currentTimeMillis();
		for(int i=0;i<20;i++){
			SerializeUtil.serialize(criteriaList.get(i));
		}
		System.out.println(System.currentTimeMillis()-start);
	}
}
